package net.vsame.url2sql.url;

import java.lang.reflect.Method;
import java.util.Map;


public class UrlConfig {
	
	private String url;//路径
	private Method method;//如果是Java, 需指定方法
	private String js;//如果是js, js不能为空
	private Map<String, String> vars;//放置需集中管理的文本，如Sql
	private String v;//校验
	private String cacheScript;//缓存脚本
	private String cleanCache;//缓存脚本
	
	/**
	 * 将文本转换为对应Method
	 * @param method
	 * @return
	 */
	public static Method string2Method(String method){
		int index = method.lastIndexOf(".");
		String className = method.substring(0, index);
		String methodName = method.substring(index+1);
		try {
			Class<?> clazz = Class.forName(className);
			return clazz.getMethod(methodName);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		if(!url.startsWith("/")){
			url = "/" + url;
		}
		this.url = url;
	}
	public Method getMethod() {
		return method;
	}
	public void setMethod(Method method) {
		this.method = method;
	}
	public void setMethod(String method) {
		this.method = string2Method(method);
	}
	public String getJs() {
		return js;
	}
	public void setJs(String js) {
		this.js = js;
	}
	public Map<String, String> getVars() {
		return vars;
	}
	public void setVars(Map<String, String> vars) {
		this.vars = vars;
	}
	public String getV() {
		return v;
	}
	public void setV(String v) {
		this.v = v;
	}
	public String getCacheScript() {
		return cacheScript;
	}
	public void setCacheScript(String cacheScript) {
		this.cacheScript = cacheScript;
	}
	public String getCleanCache() {
		return cleanCache;
	}
	public void setCleanCache(String cleanCache) {
		this.cleanCache = cleanCache;
	}
	
	/**
	 * 测试 setMethod方法
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		Method method = UrlConfig.class.getMethod("getV");
		System.out.println(method.getDeclaringClass());
		UrlConfig u = new UrlConfig();
		u.setMethod("net.vsame.url2sql.url.UrlConfig.getV");
		System.out.println(u.getMethod());
	}
	
}
